package com.bears.god.sup.servlet;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.bears.god.sup.model.Dept;
import com.bears.god.sup.service.ServiceDAOFactory;

@WebServlet("/deptlist")
public class DeptServlet extends HttpServlet {
	private static final long serialVersionUID = 1234L;

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		doPost(req, resp);

	}

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		String num = req.getParameter("id");
		if (num != null) {
			try {
				List<Dept> dept = ServiceDAOFactory.getDeptDAO().findByAll((new Integer(num) - 1) * 5, 5);
				req.getSession().setAttribute("dept", dept);
				req.getRequestDispatcher("/WEB-INF/view/manager/deptM.jsp").forward(req, resp);
			} catch (Exception e) {
				e.printStackTrace();
			}
		} else {
			try {
				List<Dept> dept = ServiceDAOFactory.getDeptDAO().findByAll(0, 5);
				req.getSession().setAttribute("dept", dept);
				req.getRequestDispatcher("/WEB-INF/view/manager/deptM.jsp").forward(req, resp);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}
}
